
// 物美多点时间戳API
function getDuoDianTime() {
    var response_duodian = http.get("https://appapis.dmall.com/static/queryUserCheckInfo.jsonp");
    var duodian_Time = response_duodian.body.string();
    var json = eval(duodian_Time)
    var duodian_Time = json.result.data.currentTime;
    var duodian_number_time = Number(duodian_Time);
    toastLog("多点时间戳为:" + duodian_number_time);
    var cur = new Date().getTime();
    toastLog("当前时间戳为:" + cur);
    toastLog("时间差：" + (duodian_number_time - cur))
    return duodian_number_time;
}

// getDuoDianTime()

// 计算下一个10点的时间戳
// 使用当前时间戳加上多点时间戳和当前时间戳的差判断是否到达10点的时间戳，到达则启动多线程
// 里面预约抢购按钮找到并点击后关闭前2个线程并启动第三个线程执行下单操作

// 获取下一个可抢购的10点的时间戳
function getNextSeckillTime() {

}

function run() {
    // 启动线程
    threads.start(function () {
        // 时间相关
        getDuoDianTime();
    });

    threads.start(function () {
        for (var i = 0; i < 10; i++) {
            console.info(i);
            sleep(50);
        }
    });
}

run();

function first() {
    while(true) {
        if(className("android.widget.Button").text("预约抢购").exists()){
            className("android.widget.Button").text("预约抢购").click();
        }
        sleep(10);
    }
}

// 打开App
function open_app(appName){
    var name = getPackageName(appName);
    if(name){
        toastLog("打开"+appName+"...");
        launch(name);
    }
    else{
        toastLog("App名称错误");
    }
    return name;
}
 
// open_app("京东");

// 等待View存在并延时点击
function wait_view_exists_click(viewName, delayTime, showMsg){
    while(true){
        if(className("android.view.View").desc(viewName).exists()){
            sleep(delayTime);
            toastLog("点击"+showMsg)
            className("android.view.View").desc(viewName).findOnce().click();
            break;
        }
    }
}
 
// 等待TextView存在并延时点击
function wait_textview_exists_click(textViewName, delayTime, showMsg){
    while(true){
        if(className("android.widget.TextView").text(textViewName).exists()){
            sleep(delayTime);
            toastLog("点击" + showMsg)
            let b = className("android.widget.TextView").text(textViewName).findOnce().bounds();
            click(b.centerX(),b.centerY());
            break;
        }
    }
}
 
// 等待TextView存在并延时点击, 如果满足条件退出循环
function wait_textview_exists_click_break(textViewName, delayTime, showMsg, textViewNameBreak, showMsgBreak){
    while(true){
        if(className("android.widget.TextView").text(textViewName).exists()){
            sleep(delayTime);
            toastLog("点击" + showMsg)
            let b = className("android.widget.TextView").text(textViewName).findOnce().bounds();
            click(b.centerX(),b.centerY());
            break;
        }
        else if(className("android.widget.TextView").text(textViewNameBreak).exists())
        {
            toastLog(showMsgBreak)
            break;
        }
    }
}
 
// 等待Button存在并点击
function wait_button_exists_click(buttonName, delayTime, showMsg){
    while(true){
        if(className("android.widget.Button").text(buttonName).exists()){
            sleep(delayTime);
            toastLog("点击" + showMsg)
            let b = className("android.widget.Button").text(buttonName).findOnce().bounds();
            click(b.centerX(),b.centerY());
            break;
        }
    }
}

// 京东抢购茅台预约
function jd_mt_qg(){
    wait_textview_exists_click("立即抢购", 0, "\"立即抢购\"");
    wait_button_exists_click("提交订单", 0, "\"提交订单\"");
}

// 定时预约抢购
function timing_jd_mt(hour_yy, min_yy, sec_yy, hour_qg, min_qg, sec_qg){
    var yy_flag = 0;    //预约成功标志
    toastLog("执行京东抢购任务...");
    while(true){
        var nowDate = new Date();
        if(nowDate.getHours()==hour_yy && nowDate.getMinutes()==min_yy && nowDate.getSeconds()==sec_yy){
            toastLog("定时时间到, 开始预约...");
            jd_mt_yy();
            yy_flag = 1;
        }
        if(nowDate.getHours()>=hour_qg && nowDate.getMinutes()>=min_qg && nowDate.getSeconds()>=sec_qg &&yy_flag){
            toastLog("定时时间到, 开始抢购...");
            jd_mt_qg();
        }
    }
}
 
// timing_jd_mt(10, 02, 00, 12, 00, 00);